
package com.chat.datea.fragment;

import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chat.datea.R;
import com.chat.datea.adapter.GuessSingerFragment_ViewPager_Adapter;
import com.chat.datea.base.BaseFragment;
import com.chat.datea.base.Finnal_Base;
import com.chat.datea.bean.SingerInfo;
import com.chat.datea.utils.volley.VolleySingleton;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

/**
 * Created by xyb on 15/10/22.
 */
public class GuessSingerFragment extends BaseFragment implements View.OnClickListener {
    private Toolbar toolbar;
    private FragmentManager fManager;
    private String sing_name;
    private ViewPager viewPager;
    private GuessSingerFragment_ViewPager_Adapter pagerAdapter;
    private TabLayout tabLayout;
    private CollapsingToolbarLayout toolbarLayout;
    private ImageView imageView;
    private String ting_uid, artist_id;
    private VolleySingleton singleton;


    public GuessSingerFragment(FragmentManager fManager, String ting_uid, String artist_id,
                               String sing_name) {
        this.fManager = fManager;
        this.sing_name = sing_name;
        this.ting_uid = ting_uid;
        this.artist_id = artist_id;
        singleton = VolleySingleton.getVolleySingleton(mActivity);

    }

    @Override
    protected void initData() {
        toolbar.setNavigationIcon(R.mipmap.nav_back);
        toolbar.setNavigationOnClickListener(this);
        toolbarLayout.setTitle(sing_name);
        toolbarLayout.setExpandedTitleColor(0xffffffff);
        toolbarLayout.setCollapsedTitleTextColor(0xffffffff);

        String singerUrl = Finnal_Base.apiUrl + Finnal_Base.method + "baidu.ting.artist.getinfo" +
                Finnal_Base.format + Finnal_Base.tinguid + ting_uid + Finnal_Base.artistid + artist_id;


        StringRequest request = new StringRequest(singerUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SingerInfo info = gson.fromJson(response, SingerInfo.class);
                Log.e("GuessSingerFragment", info.getAvatar_s1000());
                if (!(info.getAvatar_s1000()).equals("")) {

                    Picasso.with(mActivity).load(info.getAvatar_s1000()).into(imageView);
                }

                pagerAdapter = new GuessSingerFragment_ViewPager_Adapter
                        (getChildFragmentManager(), info.getTing_uid(), info.getArtist_id(), info.getAlbums_total());
                viewPager.setAdapter(pagerAdapter);
                tabLayout.setupWithViewPager(viewPager);
                tabLayout.setSelectedTabIndicatorColor(0xff0088ff);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        singleton.addQueue(request, "GuessSingerFragment");


    }


    @Override
    protected void initView() {
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        tabLayout = (TabLayout) view.findViewById(R.id.tabs);
        toolbarLayout = (CollapsingToolbarLayout) view.findViewById(R.id.collapsing_toolbar);
        imageView = (ImageView) view.findViewById(R.id.singer_img);

    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.avtivity_recommendsong, null);
        return view;
    }


    //toolbar 的返回按钮监听
    @Override
    public void onClick(View v) {
        fManager.popBackStack();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        singleton.removeRequest("GuessSingerFragment");
        Log.e("GuessSingerFragment", "GuessSingerFragment");
    }
}
